package com.zisu.aradvertising.service.impl;

import com.zisu.aradvertising.pojo.*;
import com.zisu.aradvertising.pojo.business.VideoAndCreater;
import com.zisu.aradvertising.repository.*;
import com.zisu.aradvertising.service.VideoAndCreaterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class VideoAndCreaterImpl implements VideoAndCreaterService {
    //实例化抽象类
    @Resource
    private DaoCreater daoCreater;
    @Resource
    private DaoVideo daoVideo;
    @Resource
    private DaoVideoAndCreater daoVideoAndCreater;
    @Resource
    private DaoAdvertiserSpace daoAdvertiserSpace;
    @Resource
    private DaoCooperation daoCooperation;
    @Resource
    private DaoAdvertiser daoAdvertiser;
    @Resource
    private DaoSign daoSign;
    @Resource
    private DaoProduct daoProduct;
    @Resource
    private DaoOthers daoOthers;


    //返回单个视频广告商
    @Override
    public VideoAndCreater findbyvidService(int vid) {
        Video video = daoVideo.findByVid(vid);
        VideoAndCreater videoAndCreater = new VideoAndCreater();
        videoAndCreater.setVid(video.getVid());
        videoAndCreater.setVreleasetime(video.getVreleasetime());
        videoAndCreater.setVintroduction(video.getVintroduction());
        videoAndCreater.setVstyle(video.getVstyle());
        videoAndCreater.setVrequirement(video.getVrequirement());
        videoAndCreater.setVadtime(video.getVadtime());
        videoAndCreater.setVsumcollection(video.getVsumcollection());
        videoAndCreater.setVsource(video.getVsource());
        videoAndCreater.setCid(video.getVid());
        videoAndCreater.setVname(video.getVname());
        videoAndCreater.setFengmian(video.getFengmian());
        Creater creater = daoCreater.findByCid(videoAndCreater.getCid());
        creater.setOthers(daoOthers.findAllByOther(creater.getCother()));
        videoAndCreater.setCreater(creater);
        return videoAndCreater;
    }

    @Override
    //返回所有视频和视频创作者信息
    public List<VideoAndCreater> findallService() {
        List<Video> videoList = daoVideo.findAll();
        System.out.println(videoList.size());

        List<VideoAndCreater> list = new ArrayList<>();
        for (Video x : videoList) {
            System.out.println(x.toString());
            VideoAndCreater videoAndCreater = new VideoAndCreater();
            System.out.println(1);
            videoAndCreater.setVid(x.getVid());
            System.out.println(2);
            videoAndCreater.setVreleasetime(x.getVreleasetime());
            System.out.println(3);
            videoAndCreater.setVintroduction(x.getVintroduction());
            System.out.println(4);
            videoAndCreater.setVstyle(x.getVstyle());
            videoAndCreater.setVrequirement(x.getVrequirement());
            videoAndCreater.setVadtime(x.getVadtime());
            videoAndCreater.setFengmian(x.getFengmian());
            videoAndCreater.setVsumcollection(x.getVsumcollection());
            videoAndCreater.setVsource(x.getVsource());
            videoAndCreater.setCid(x.getVid());
            videoAndCreater.setVname(x.getVname());
            Creater creater = daoCreater.findByCid(x.getCid());
            if (daoOthers.findAllByOther(creater.getCother()) != null) {
                creater.setOthers(daoOthers.findAllByOther(creater.getCother()));
            }
            videoAndCreater.setCreater(creater);
            System.out.println("ok" + videoAndCreater.toString());
            list.add(videoAndCreater);
        }
        return list;
    }

    //返回一个创作下的所有视频和广告位信息
    @Override
    public List<VideoAndCreater> findallbycidService(int cid) {
        //找到所有的视频
        System.out.println("0");
        List<VideoAndCreater> list = daoVideoAndCreater.findAllByCid(cid);
        System.out.println("00");
        for (VideoAndCreater x : list) {
            //找到该视频下的所有广告位信息
            List<AdvertiserSpace> list1 = daoAdvertiserSpace.findAllByVid(x.getVid());
            System.out.println("1");
            List<Advertiser> l1 = new ArrayList<>();
            for (AdvertiserSpace x1 : list1) {
                System.out.println("2");
                //找出每个广告位对应的合作信息
                x1.setCooperations(daoCooperation.findAllByApid(x1.getApid()));
                System.out.println("3");
                //找出每个合作对应的广告商
                for (Cooperation c : x1.getCooperations()) {
                    System.out.println("4");
                    Sign sign = daoSign.findByCoid(c.getCoid());
                    System.out.println("5");
                    c.setAdvertisername(daoAdvertiser.findByAid(sign.getAid()).getAname());
                    System.out.println("6");
                    c.setProductname(daoProduct.findByPid(c.getPid()).getPname());
                    System.out.println("7");
                }
            }
            x.setAdvertiserSpaceList(list1);
        }

        return list;
    }


}
